﻿using System;

namespace HNAlbum.Helper
{
    public class Function
    {
        public static string GetRandomPassword(int length)
        {
            char[] chars = "abcdefghijklmnopqrstuvwxyz1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray();
            string password = string.Empty;
            Random random = new Random();

            for (int i = 0; i < length; i++)
            {
                int x = random.Next(1, chars.Length);
                //Don't Allow Repetation of Characters
                if (!password.Contains(chars.GetValue(x).ToString()))
                    password += chars.GetValue(x);
                else
                    i--;
            }
            return password;
        }

        /// <summary>
        /// Returns a truncated string with a desire length
        /// </summary>
        /// <param name="input">Input string</param>
        /// <param name="length">Desire length</param>
        /// <returns></returns>
        public static string TruncateString(string input, int length)
        {
            if (input.Length < length)
                return input;
            else
                return input.Substring(0, length - 3) + "...";
        }

        /// <summary>
        /// Get the paging page count
        /// </summary>
        /// <param name="itemPerPage">Number of items per page</param>
        /// <param name="totalItems">Total items count</param>
        /// <returns></returns>
        public static int GetPageCount(int itemPerPage, int totalItems)
        {
            //Page count
            int pageCount = 0;
            if (totalItems % itemPerPage == 0)
                pageCount = totalItems / itemPerPage;
            else
                pageCount = (totalItems / itemPerPage) + 1;

            return pageCount;
        }
    }
}
